<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2016/8/14
 * Time: 17:46
 */

namespace J\Web;
use J\Message;

/**
 * URl 类
 *    构造URL
 *    存取上一次URL
 */
class Url
{
    const URI_SESSION_KEY = '__lastUri';

    // www.your.com/admin/index.php/path/info.html?a=a
    protected $uri = null;

    // /admin/index.php
    protected $scriptUrl;

    // TODO 组合一个全面的uri Http(s) HOST PORT ScriptUrl

    /**
     * @var string
     */
    // protected $baseUrl;
    /**
     * @var string
     */
    // protected $script;


    public function push()
    {
        Session::set(static::URI_SESSION_KEY,$this->getRequestUri());
    }

    public function pop($default = '')
    {
        $url = Session::get(static::URI_SESSION_KEY);
        Session::del(static::URI_SESSION_KEY);
        if (null === $url) $url = $default;
        return $url;
    }

    /**
     * 返回 /admin/index.php
     * @return string
     * @throws \Exception
     */
    public function getScriptUrl()
    {
        if ($this->scriptUrl === null) {
            $scriptFile = $this->getScriptFile();
            $scriptName = basename($scriptFile);
            if (basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
                $this->scriptUrl = $_SERVER['SCRIPT_NAME'];
            } elseif (basename($_SERVER['PHP_SELF']) === $scriptName) {
                $this->scriptUrl = $_SERVER['PHP_SELF'];
            } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
                $this->scriptUrl = $_SERVER['ORIG_SCRIPT_NAME'];
            } elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) {
                $this->scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName;
            } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) {
                $this->scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFile));
            } else {
                throw new \Exception(Message::SCRIPT_NOT_FIND);
            }
        }

        return $this->scriptUrl;
    }

    /**
     * 返回 real/path/to/admin/index.php
     *
     * @return string
     */
    public function getScriptFile()
    {
        return isset($this->scriptFile) ? $this->scriptFile : $_SERVER['SCRIPT_FILENAME'];
    }

    /**
     * 请求URL
     * @return string
     */
    public function getRequestUri()
    {
        if (!$this->uri){
            if (isset($_SERVER['REQUEST_URI'])) {
                $uri = $_SERVER['REQUEST_URI'];
            }
            elseif (isset($_SERVER['argv'])) {
                $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
            }
            else {
                $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
            }
            $this->uri = $uri;
        }

        return $this->uri;
    }

    /**
     * @param \Throwable $e
     */
    public function exceptionView(\Throwable $e)
    {
        include DEFAULT_VIR. DS . 'think_exception.php.tpl';
    }

    public function slashView($var)
    {
        extract($var);
        include DEFAULT_VIR. DS . 'jump.php.tpl';
    }
}